home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1903 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.6 KB  |  88 lines

  1. Newsgroups: comp.lang.c
  2. Path: in1.uu.net!bcstec!schultz
  3. From: schultz@bcstec.ca.boeing.com (Robert A. Schultz)
  4. Subject: Re: Newbie question:  Is this code OK?
  5. Message-ID: <DLC72M.H4v@bcstec.ca.boeing.com>
  6. Organization: The Boeing Company
  7. X-Newsreader: TIN [version 1.2 PL2]
  8. References: <4di986$fk1@pegasus.interpac.net>
  9. Date: Wed, 17 Jan 1996 17:54:22 GMT
  10.  
  11. Stacy Sherman (stacys@isis.interpac.net) wrote:
  12. : I'm learning C and would like to know your opinions on my answer to an 
  13. : exercise.  I was supposed to write a function that counts the number of 
  14. : words in a string (array of char in this case, with the length specified 
  15. : in  a variable).  Of course the biggest problem is stripping out all the 
  16. : extraneous white space that may be there.  In this exercise, whitespace 
  17. : separating words was either a space, tab or newline character.
  18.  
  19. Thanks for writing the code and then asking for opinions.
  20.  
  21. : The code I wrote worked on every case I gave it but I want to know if it 
  22. : was well written.  Specifically:
  23.  
  24. : I know it's usually not a good idea to increment a loop counter within a 
  25. : loop, but I have 2 nested loops using the same counter.  Was this a bad 
  26. : thing to do?
  27.  
  28. Not really a bad idea provided you are careful that the end conditions of
  29. both loops do not conflict with each other.  In your case you do check
  30. i<length is both so I would have no problem with this code.  Personal
  31. preference would have led me to use a while loop in the second case but
  32. that is just cosmetic.
  33.  
  34. : I have an if statement and a for loop that test for the same things.  Is 
  35. : it possible to somehow combine these into one statement?  I couldn't 
  36. : think of a way to do it.  
  37.  
  38. To be honest nothing leaps to my mind right of the top of my head either.
  39.  
  40. : Do I need the empty {} after a for loop that has no body?  I assumed if I 
  41. : didn't, the loop would execute the next statement after it. 
  42.  
  43. No.  The empty {} is not required.  Simply replace it with a ';' and all will
  44. be fine.  Again I would consider this to be a cosmetic change.
  45.  
  46. : Any other comments?  Here's the code:
  47.  
  48. 1) So what happens if length is > 80?  Unless you are *very* sure of you
  49.    input data you should always check for your string being terminated
  50.    in some unexpected place.  You could simply watch for string[i] = '\0'
  51.    in your case.  The moral of the story here is that careful checking of 
  52.    input data will save you a lot of pain down the road.
  53. 2) I am not sure of the requirements of the assignment but I would have
  54.    written the "char string[80]" in the declaration as a more general
  55.    "char *string" or "char string[]".  Of course I would then also add
  56.    a check for "string == NULL"
  57. 3) You can check for string[i] being '\n', '\t' or ' ' by calling isspace().
  58.    E.g. "if (!isspace(string[i])) ...
  59. 4) It looks to me like you have a grasp of what is going on.
  60.  
  61.  
  62. : int words (char string[80], int length)
  63. : {
  64. :     int i, num_words = 0;
  65. :     
  66. :     for (i=0; i<length; i++)
  67. :     {                /* if char isn't whitespace */
  68. :         if ((string[i]!='\n') && 
  69. :             (string[i]!='\t') &&
  70. :             (string[i]!=' '))
  71. :         {
  72. :             num_words++;        /* Increment num_words */
  73. :             for (; i<length, ((string[i]!='\n') &&
  74. :                 (string[i]!='\t') &&   
  75. :                 (string[i]!=' ')); i++) 
  76. :                  /* and skip past any other */
  77. :                  /* chars that may be there */
  78. :             { }    /* loop does all work, nothing inside body */
  79. :         }
  80. :     }
  81. :     return (num_words);
  82. : }
  83. -- 
  84. ='''                                            Rob Schultz
  85. c-oo                                            TSAP Programmer Guy
  86.    \                                            Boeing Tech Services Systems
  87.   -    veni vidi recodei                        (206) 544-0793 MS 2H-31
  88.